package net.zjitc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.common.ResponseResult;
import net.zjitc.common.page.ManagerPageData;
import net.zjitc.entity.Manager;
import net.zjitc.entity.Vo.ManagerVo;
import net.zjitc.exception.ManagerException;
import net.zjitc.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    @PreAuthorize("hasAuthority('136')")
    @GetMapping("/users")
    public ResponseResult getUsers(@RequestParam(required = false) String query,
                                   @RequestParam Integer pagenum,
                                   @RequestParam Integer pagesize){
        Page page = managerService.selectPage(query,pagenum, pagesize);
        ManagerPageData managerPageData =new ManagerPageData();
        managerPageData.setUsers(page.getRecords());
        managerPageData.setPagenum(page.getCurrent());
        managerPageData.setTotal(page.getTotal());
        ResponseResult result = new ResponseResult();
        result.setSuccess("查询成功", managerPageData);
        return result;
    }

    @PreAuthorize("hasAuthority('131')")
    @PostMapping("/users")
    public ResponseResult addUsers(@RequestBody ManagerVo managerVo){
        managerVo.setRid(0);
        managerVo.setMg_state(true);
        return managerService.addUsers(managerVo);
    }


    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('136')")
    @GetMapping("/users/{id}")
    public ResponseResult selectById(@PathVariable Integer id){
        return managerService.selectById(id);
    }

    /**
     * 修改用户状态
     * @param uid
     * @param type
     * @return
     */
    @PreAuthorize("hasAuthority('159')")
    @PutMapping("/users/{uid}/state/{type}")
    public ResponseResult updateType(@PathVariable Integer uid,
                                       @PathVariable Boolean type) {
        return managerService.updateType(uid, type);
    }


    //编辑用户提交  133
    @PreAuthorize("hasAuthority('133')")
    @PutMapping("/users/{id}")
    public ResponseResult updateUser(@PathVariable Long id, @RequestBody(required = false) Manager manager){
        return managerService.updateUser(id,manager);
    }

    /**
     * 删除单个用户
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('132')")
    @DeleteMapping("/users/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id) {
        return managerService.deleteUser(id);
    }


    //分配用户角色接口  137
    @PreAuthorize("hasAuthority('137')")
    @PutMapping("/users/{id}/role")
    public ResponseResult giveManagerRole(@PathVariable Long id,@RequestBody Manager manager){
        ResponseResult<Manager> res;
        try{
            res=managerService.giveManagerRole(id,manager);
        }catch (ManagerException e){
            res=new ResponseResult<>();
            res.setError("设置角色失败");
        }
        return res;
    }

}
